#include <Measure.h>

ostream & operator << (ostream & os, Measure & quantity)
{
	if (LENGTH == quantity.getMeasureType())
	{
		quantity.printLength(&os);
	}
	else if (VOLUME == quantity.getMeasureType())
	{
		quantity.printVolume(&os);
	}

	return os;
}


Measure::Measure(MEASURE_TYPE measure, unsigned int quantity, LENGTH_TYPE type):unit(measure, type)
{
    m_absQuantity = quantity * unit.getRatio();
}


Measure::Measure(MEASURE_TYPE measure, unsigned int quantity, VOLUME_TYPE type):unit(measure, type)
{
    m_absQuantity = quantity * unit.getRatio();
}


bool Measure::operator == (const Measure & quantity) const
{
    return ((unit == quantity.unit) && (m_absQuantity == quantity.m_absQuantity));
}


Measure Measure::operator + (Measure & quantity) const
{
    if (LENGTH == quantity.getMeasureType())
    {
	Measure tmpQuantity(LENGTH, (m_absQuantity + quantity.m_absQuantity), INCH);

	return tmpQuantity;
    }
    else if (VOLUME == quantity.getMeasureType())
    {
	Measure tmpQuantity(VOLUME, (m_absQuantity + quantity.m_absQuantity), TBSP);

	return tmpQuantity;
    }
}


MEASURE_TYPE Measure::getMeasureType()
{
    return unit.getMeasureType();
}


void Measure::printLength(ostream *os)
{
	unsigned int tmpLen = m_absQuantity;
	unsigned int tmpInch, tmpFeet, tmpYard, tmpMile;

	tmpMile = tmpLen / (YARD_PER_MILE * FEET_PER_YARD * INCH_PER_FEET);
	tmpLen = tmpLen % (YARD_PER_MILE * FEET_PER_YARD * INCH_PER_FEET);

	tmpYard = tmpLen / (FEET_PER_YARD * INCH_PER_FEET);
	tmpLen = tmpLen % (FEET_PER_YARD * INCH_PER_FEET);

	tmpFeet = tmpLen / (INCH_PER_FEET);
	tmpInch = tmpLen % (INCH_PER_FEET);

	if (tmpMile)  *os << tmpMile << " Mile ";
	if (tmpYard)  *os << tmpYard << " Yard ";
	if (tmpFeet)  *os << tmpFeet << " Feet ";
	if (tmpInch)  *os << tmpInch << " Inch ";

	*os << " = " << m_absQuantity << " Inch";	
	*os << '\n';
}


void Measure::printVolume(ostream *os)
{
	unsigned int tmpVol = m_absQuantity;
	unsigned int tmpTbsp, tmpTsp, tmpOz;

	tmpOz = tmpVol / (TBSP_PER_OZ * TSP_PER_TBSP);
	tmpVol = tmpVol % (TBSP_PER_OZ * TSP_PER_TBSP);

	tmpTsp = tmpVol / (TSP_PER_TBSP);
	tmpTbsp = tmpVol % (TSP_PER_TBSP);

	if (tmpOz)  *os << tmpOz << " OZ ";
	if (tmpTsp)  *os << tmpTsp << " TSP ";
	if (tmpTbsp)  *os << tmpTbsp << " TBSP ";

	*os << " = " << m_absQuantity << " TBSP";
	
	*os << '\n';

}

